<?php

include('logincheck.php'); 

$timezonemap = array(
	'-10.0' => '(GMT -10:00) Hawaii',
	'-9.0' => '(GMT -9:00) Alaska',
	'-8.0' => '(GMT -8:00) Pacific Time',
	'-7.0' => '(GMT -7:00) Mountain Time',
	'-6.0' => '(GMT -6:00) Central Time',
	'-5.0' => '(GMT -5:00) Eastern Time'
);

$notificationTimeMap = array('1 hour', '1 day', '2 days', '3 days');

$result = mysql_query("SELECT * FROM notifications WHERE facebookid='" . $_SESSION['facebookid'] . "'");
$pendingNotificationRows = "";
while ($row = mysql_fetch_array($result)) {
	
	$notificationTimes = array();
	for($i = 0; $i < strlen($row['notifydate']); $i++){
		if ($row['notifydate'][$i] != '0') 
			$notificationTimes[] = $row['notifydate'][$i] == '1' ? $notificationTimeMap[$i] : '<span style="text-decoration:line-through">'.$notificationTimeMap[$i].'</span>';
	}	
	
	$contactPoints = array();
	if ($row['sendsms'] == 1) $contactPoints[] = preg_replace('/([0-9]{3})([0-9]{3})([0-9]{4})/', '($1) $2-$3', $row['cellnumber']);
	if ($row['sendemail'] == 1) $contactPoints[] = $row['emailaddress'];
	
	$pendingNotificationRows .= '<tr onclick="window.location=\'/entry.php?edit=' . $row['entryid'] . '\';">'
		. '<td>' . $row['clientnamefirst'] . ' ' . $row['clientnamelast'] . '</td>'
		. '<td>' . implode(', ', $contactPoints) . '</td>'
		. '<td>' . implode(', ', $notificationTimes) . '</td>'
		. '<td>' . date('F jS\, Y\, h:i A', $row['appointmentdate']) . '</td>'
		. '<td>' . $row['appointmentbody'] . '</td>'
		. '<td><a href="/entry.php?edit=' . $row['entryid'] . '">Edit</a> / <a href="/entry.php?delete=' . $row['entryid'] . '">Delete</a></td>'
		. '</tr>';
}

$timezone = $timezonemap[$timezone];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>SMS Notification System</title>
</head>

<body>

    <div id="content">
    
        <div id="details">
            <span class="accountdetail">Your account id: </span><? echo $_SESSION['facebookid']; ?><br />
            <span class="accountdetail">Email: </span><? echo $emailaddress; ?><br />
            <span class="accountdetail">Company: </span><? echo $company; ?><br />
            <span class="accountdetail">Timezone: </span><? echo $timezone; ?><br />
            <button id="editprofile" onclick="window.location = '/profile.php?edit'">Edit Profile</button>
        </div>


		<div id="createnew"><button id="createnewbutton" onclick="window.location = '/entry.php'">Create new appointment notification</button></div>

		<div id="notifications">
            <h2>Appointment Notifications</h2>
            <div id="tablewrapper">
                <table id="statstable">
                    <thead>
                        <tr>
                            <th>Client Name</th>
                            <th>Contact</th>
                            <th>Notification Time(s)</th>
                            <th>Date</th>
                            <th>Appointment</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php echo $pendingNotificationRows; ?>
                    </tbody>
                </table>
            </div>
        </div>

</div>

</body>
</html>